package com.zhitong.money.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.Holiday;
import com.zhitong.money.exception.AddException;
import com.zhitong.money.exception.DeleteException;
import com.zhitong.money.exception.RepeatException;
import com.zhitong.money.exception.UpdateException;
import com.zhitong.money.mapper.HolidayMapper;
import com.zhitong.money.services.api.IHolidayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class HolidayService implements IHolidayService {

    @Autowired
    private HolidayMapper mapper;

    @Override
    public List<Holiday> findAllHoliday() throws Exception {
        List<Holiday> allHoliday = mapper.findAllHoliday();
        if (allHoliday==null || allHoliday.isEmpty())
            throw new NullPointerException("findAllHoliday:查询为空!");
        return allHoliday;
    }

    @Override
    public Holiday fidHolidayById(String id) throws Exception {
        Holiday holidayById = mapper.findHolidayById(id);
        if (holidayById==null){
            throw new NullPointerException("findHolidayById:查询为空");
        }
        return holidayById;
    }

    @Override
    public PageInfo findHolidayByPage(Map<String,Object> map) throws Exception {
        int pageNum=map.containsKey("pageNum")?(int)map.get("pageNum"):1;
        int pageSize=map.containsKey("pageSize")?(int)map.get("pageSize"):5;
        PageHelper.startPage(pageNum,pageSize);
        List<Holiday> allHoliday = mapper.findHolidayByMap(map);
        if (allHoliday==null ||allHoliday.isEmpty()){
            throw new NullPointerException("findHolidayByPage:查询为空");
        }
        PageInfo pageInfo=new PageInfo(allHoliday);
        return pageInfo;
    }

    @Override
    public List<Holiday> findHolidayByMap(Map<String, Object> map) throws Exception {
        List<Holiday> holidayByMap = mapper.findHolidayByMap(map);
        if (holidayByMap==null || holidayByMap.isEmpty()){
            throw new NullPointerException("findHolidayByMap:查询为空");
        }
        return holidayByMap;
    }


    @Override
    public void addHoliday(Holiday holiday) throws Exception {
        Holiday holidayByName = mapper.findHolidayByName(holiday.getName());
        if (holidayByName!=null){
            throw new RepeatException("该假期已存在!");
        }
        try {
            mapper.addHoliday(holiday);
        } catch (Exception e) {
            throw new AddException("addHoliday:添加失败!"+e.getMessage());
        }
    }

    @Override
    public void updateHoliday(Holiday holiday) throws Exception {
        Holiday holidayByName = mapper.findHolidayByName(holiday.getName());
        if (holidayByName!=null && !holidayByName.getId().equals(holiday.getId())){
            throw new RepeatException("该假期已存在!");
        }
        try {
            mapper.updateHoliday(holiday);
        } catch (Exception e) {
            throw new UpdateException("updateHoliday:更新失败"+e.getMessage());
        }
    }

    @Override
    public void delHoliday(String id) throws Exception {
        try {
            mapper.delHoliday(id);
        } catch (Exception e) {
            throw new DeleteException("delHoliday:删除失败"+e.getMessage());
        }
    }

    @Override
    public void delHolidayByIds(List<String> ids) throws Exception {
        try {
            mapper.delHolidayByIds(ids);
        } catch (Exception e) {
            throw new DeleteException("delHolidayByIds:删除失败"+e.getMessage());
        }
    }
}
